//
//  XMLUserAcount.swift
//  Weibo
//
//  Created by mac on 16/5/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class XMLUserAcount: NSObject, NSCoding {

    var access_token: String?
    var expires_in: NSTimeInterval = 0 {
        didSet {
            expiresDate = NSDate().dateByAddingTimeInterval(expires_in)
        }
    }
    var expiresDate :NSDate?
    var uid :Int64 = 0
    //用户头像
    var avatar_large: String?
    var name: String?
    
    init(dict: [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    override var description: String {
    
        let keys = ["access_token","expires_in","uid","expiresDate","name","avatar_large"]
        return dictionaryWithValuesForKeys(keys).description
    }
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeInt64(uid, forKey: "uid")
        aCoder.encodeObject(name, forKey: "name")
    }
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        uid = aDecoder.decodeInt64ForKey("uid")
        name = aDecoder.decodeObjectForKey("name") as? String
    }
    func saveUserAcount() -> Bool {
    
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
//        print(path)
        
        return NSKeyedArchiver.archiveRootObject(self, toFile: path)
        
    }
    class func loadUserAcount() -> XMLUserAcount? {
    
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
        return NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? XMLUserAcount
        
    }
}
